package com.hades.xq.ares.titlebar;


import android.app.Activity;
import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.v4.app.Fragment;
import android.text.InputFilter;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * Created by wellor on 2017/3/28. 通用型 titlebar
 */

public class BaseTitleBar {
    protected final Activity mContext;
    protected TextView mCenterTv;
    protected View mTitleBack;
    protected TextView mRightTv;
    protected ImageView mRightIv;

    protected View mRightBtn;

    protected ImageView mLeftIv;
    protected View.OnClickListener mRightListener;

    /**
     * 返回功能 实现即可
     */
    public interface OnBack {

    }

    public BaseTitleBar(Context context) {
        mContext = (Activity) context;
        init(mContext.getWindow().getDecorView());
    }

    public BaseTitleBar(Fragment f) {
        mContext = f.getActivity();
        init(f.getView());
    }

    public BaseTitleBar(View v) {
        mContext = (Activity) v.getContext();
        init(v);
    }

    private void init(View decorView) {
        mCenterTv = (TextView) decorView.findViewById(R.id.title_text);
        mTitleBack = decorView.findViewById(R.id.title_back);
        mLeftIv = decorView.findViewById(R.id.leftIv);
        mRightTv = (TextView) decorView.findViewById(R.id.rightTv);
        String title = (String) mContext.getTitle();
        setCenter(title);
        mRightBtn = decorView.findViewById(R.id.rightBtn);
        mRightIv = (ImageView) decorView.findViewById(R.id.rightIv);

        if (mContext instanceof OnBack) {
            mTitleBack.setVisibility(View.VISIBLE);
        }
        mTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBack();
            }
        });
    }

    public void onBack() {
        if (mContext instanceof OnBack) {
            mContext.finish();
        }
    }


    public View.OnClickListener onRightClick() {
        return mRightListener;
    }

    public void setCenter(CharSequence str) {
        String title = str.toString();
        if (str.length() > 10) {
            title = (String) str.subSequence(0, 7) + "...";
        }
        mCenterTv.setText(title);
    }


    public void setCenter(SpannableStringBuilder str) {
        mCenterTv.setFilters(new InputFilter[] { new InputFilter.LengthFilter(20) });
        mCenterTv.setText(str);
    }


    public void setRight(String str) {
        setRight(-1, str, null);
    }

    public void setRight(String str, View.OnClickListener l) {
        setRight(-1, str, l);
    }

    public void setRight(int imgId, View.OnClickListener l) {
        setRight(imgId, null, l);
    }

    public void setRight(int imgId, String str, View.OnClickListener l) {
        mRightBtn.setVisibility(View.VISIBLE);
        if (imgId != -1) {
            mRightIv.setVisibility(View.VISIBLE);
            mRightIv.setImageResource(imgId);
        }
        if (!TextUtils.isEmpty(str)) {
            mRightTv.setVisibility(View.VISIBLE);
            mRightTv.setText(str);
        }
        if (l != null) {
            mRightListener = l;
            mRightBtn.setOnClickListener(l);
        }
    }

    public View getLeft() {
        return mTitleBack;
    }

    public void setLeftIv(@DrawableRes int res) {
        mLeftIv.setImageResource(res);
    }

    public void setRightTextColor(int resId) {
        mRightTv.setTextColor(resId);
    }

    public TextView getRightTv() {
        return mRightTv;
    }

    public ImageView getRightIv() {
        return mRightIv;
    }

    public TextView getCenterTv() {
        return mCenterTv;
    }

}
